package com.jeesite.modules.sd.common.util;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;

import java.io.IOException;

/**
 * @ClassName PDFFontUtils
 * @Description 设置PDF字体格式
 * @Author 湛承俊
 * @Date 2020/8/10/0010 14:33:29
 * @Version 1.0
 */
public class PdfFontUtils {
    /**
     * 字体
     */
    private static BaseFont baseFont = null;

    static {
        try {
            /**
             * 设置字体 使用jar包中的字体
             */
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文档超级  排版
     *
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text) {
        Font font = new Font(baseFont);
        switch (type) {
            case 1:
            case 2:
                // 2-标题一
                // 1-标题
                font.setSize(16f);
                font.setStyle(Font.BOLD);
                break;
            case 3:
                // 3-标题二
                font.setSize(14f);
                font.setStyle(Font.BOLD);
                break;
            case 4:
                // 4-标题三
                font.setSize(14f);
                break;
            default:
                //默认大小
                font.setSize(10.5f);
                break;
        }

        //注： 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        switch (type) {
            case 1:
                //居中
                paragraph.setAlignment(Paragraph.ALIGN_CENTER);
                //上间距
                paragraph.setSpacingBefore(10f);
                //下间距
                paragraph.setSpacingAfter(10f);
                break;
            case 2:
                //默认
                paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
                paragraph.setSpacingBefore(2f);
                paragraph.setSpacingAfter(2f);
                break;
            case 3:
                paragraph.setSpacingBefore(2f);
                paragraph.setSpacingAfter(1f);
                break;
            case 4:
                paragraph.setSpacingBefore(2f);
                paragraph.setSpacingAfter(2f);
                break;
            case 5:
                paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
                paragraph.setFirstLineIndent(24);
                paragraph.setSpacingBefore(1f);
                paragraph.setSpacingAfter(1f);
                break;
            case 6:
                paragraph.setAlignment(Element.ALIGN_LEFT);
                paragraph.setSpacingBefore(1f);
                paragraph.setSpacingAfter(1f);
                break;
            default:
                break;
        }
        return paragraph;
    }
}
